package com.kaigejava.webmaginc.pipelinesimpledemo;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @author kaigejava
 */
public class PipelineSimpleDemo1 implements PageProcessor {
    public void process(Page page) {
        //https://blog.csdn.net/weixin_44477805/article/details/104573634
        page.getHtml().links().regex("https://blog.csdn.net/[a-z 0-9 -]+/article/details/[0-9]{8}").all();
        page.putField("title",page.getHtml().xpath("//*[@id=\"mainBox\"]/main/div[1]/div/div/div[1]/h1"));
    }

    public Site getSite() {
        return Site.me().setSleepTime(100).setRetryTimes(3);
    }

    public static void main(String[] args) {
        Spider.create(new PipelineSimpleDemo1())
                .addUrl("https://blog.csdn.net")
                .addPipeline( new ConsolePipeline())
                .addPipeline(new FilePipeline("C:\\data\\pipe"))
                .run();
    }
}
